<?
ini_set('display_errors',1);
define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';

drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Class Karabas_Export
{
var $domain_url="http://arm.frontmanager.com.ua";
var $__pid="aLxxqPoctjko0A2WV2GnRQ";
var $login="info_kas1";
var $password="info_kassa";
var $auth;
	function Karabas_Export()
	{
		$out = $this->connect($this->domain_url.'/LoginCommand.cmd',"login=".$this->login."&password=".$this->password."&__pid=".$this->__pid);
		
		$arr=explode(",",$out);
		$arr1=explode(":",trim($arr[1]));
		
		$this->auth= substr(trim($arr1[1]),1,strlen($arr1[1])-3);
	
		$result_xml=$this->connect($this->domain_url.'/GetPartnerEventsCommand.cmd',"__auth=".$this->auth."&__pid=".$this->__pid."&format=xml&OnlyPrintable=1");
		#."&from=".date('Ymd',strtotime("-1 months"))."&to=".date('Ymd',strtotime("+1 month"))
		#."&to=".date('Ymd',strtotime("+3 months")
		print_r($this->parse_karabas_xml($result_xml));
		
		
	}
	function connect($url,$postdata)
	{
	
		if( $curl = curl_init() ) 
		{
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
		$out = curl_exec($curl);
		curl_close($curl);
		return $out;
		}
	}
function chk_event_exists($item)
{
return false;
}
function save_event($item)
{
#print ($item->ActivityName);
/*
$node=(object) NULL;
$node->type = 'event';
$node->uid=0;
$node->title=(string)$item->ActivityName;
$node->body=(((bool)$item->ReplaceActivityDescription)?(string)$item->EventDescription:(string)$item->ActivityDescriptionLong);

$node->created = strtotime("now");
$node->changed = strtotime("now");
$node->status = 0;
$node->comment = 0;
$node->promote = 0;
$node->moderate = 1;
$node->sticky = 0;
$node->language = 'ru';
node_object_prepare($node);
node_save($node);
$event_nid=$node->nid;
*/
$event_nid=21242;



$newNode = (object) NULL;
        $newNode->type = 'schedule';
        $newNode->uid = 0;
        $newNode->title = 'Расписание #' . time();
        $newNode->created = strtotime("now");
        $newNode->changed = strtotime("now");
        $newNode->status = 1;
        $newNode->comment = 0;
        $newNode->promote = 0;
        $newNode->moderate = 0;
        $newNode->sticky = 0;
        $newNode->language = 'ru';

       # $newNode->field_schedule_place[LANGUAGE_NONE][0]['nid'] = $_REQUEST['place'];
        $newNode->field_schedule_event[LANGUAGE_NONE][0]['nid'] = $event_nid;
        $newNode->field_schedule_start[LANGUAGE_NONE][0]['value'] = date('Y-m-d H:i:s',strtotime((string)$item->EventDate));

        /*if (isset($_REQUEST['end_date']) && $_REQUEST['end_date'] != '') {
            $newNode->field_schedule_end[LANGUAGE_NONE][0]['value'] = $_REQUEST['end_date'];
        }
        else {*/
            $newNode->field_schedule_end[LANGUAGE_NONE][0]['value'] = null;
        //}
		
		#$price=0;
		if(count($item->Statistic->item)>1)
		{
		for($a=0;$a<count($item->Statistic->item);$a++)
			{
			#print $item->Statistic->item[$a]->Price."<hr>";
			$prices[]=(int)$item->Statistic->item[$a]->Price;
			}
		$price='От '.min($prices).' гр. - До '.max($prices).' гр.';	
		}else{
		$price=(int)$item->Statistic->item[0]->Price.' гр.';
		}
		#print $price."<hr>";
		#print (string)$item->HallName;
        $newNode->field_schedule_price[LANGUAGE_NONE][0]['value'] = $price;
		$newNode->field_inet_bilet[LANGUAGE_NONE][0]['value'] = null;
		$newNode->field_karabas[LANGUAGE_NONE][0]['value'] = (int)$item->EventId;
		
		print_r($newNode);
        #node_object_prepare($newNode);

        #node_save($newNode);

die();
}
function parse_karabas_xml($contents) {
$contents=str_replace('<?xml version="1.0" encoding="utf-16"?>','<?xml version="1.0" encoding="utf-8"?>',$contents);


$xml=new SimpleXMLElement($contents);
#print "<pre>";
#print_r($xml->Events->item[0]);
#print count($xml->Events->item);
	for($i=0;$i<count($xml->Events->item);$i++)
		{
		
		
		if($this->chk_event_exists($xml->Events->item[$i])==false)
		{
			$this->save_event($xml->Events->item[$i]);
		}
		$item=$xml->Events->item[$i];
		print $item->ActivityId;
		print "|";
		print $item->City;
		print "|";
		print $item->CityId;
		print "|";
		print $item->BuildingId;
		print "|";
		print $item->BuildingName;	
		print "|";
		print $item->HallId;
		print "|";
		print $item->HallName;
		print "|";
		print $item->ActivityName;
		print "|";
		print $item->ActivityCode;
		print "|";
		print $item->Categories;
		print "|";
		print $item->CategoriesIDs;
		print "|";
		print $item->EventName;
		print "|";
		print $item->EventDate;
		print "|";
		print $item->EventId;
		print "|";
		print $item->EventCode;
		print "|";
		print $item->Percent;
		print "|";
		print $item->OrganizerId;
		print "|";
		print $item->Organizer;
		print "|";
		print $item->OrganizerShortName;
		print "|";
		print $item->EventImageSmall;
		print "|";
		print $item->EventImageMedium;
		print "|";
		print $item->EventImageBig;
		print "|";
		print $item->EventImageLarge;
		print "|";
		print $item->ActivityImageSmall;
		print "|";
		print $item->ActivityImageMedium;
		print "|";
		print $item->ActivityImageBig;
		print "|";
		print $item->ActivityImageLarge;
		print "|";
		print $item->ActivityDescriptionLong;
		print "|";
		print $item->EventDescription;
		print "|";
		print $item->ReplaceActivityDescription;
		print "|";
		print $item->HasPNG;
		print "|";
		print $item->HasSVG;
		print "|";
		print $item->SVG;
		print "|";
		print $item->IsPacket;
		print "|";
		print $item->EventType;
		print "|";
		print $item->ReplaceAvailabeTicketsCount;
		print "|";
		print $item->ReplaceAvailabeTicketsCountWith;
		print "|";
		print $item->SaleStatus;
		print "|";
		print $item->SaleStatusDescription;
		print "|";
		print $item->SalesEndTime;
		print "|";
		print $item->ReserveEndTime;
		print "|";
		print $item->ETicketEnabled;
		print "|";
		print $item->PrintEnabled;
		print "|";
		print $item->Statistic;
		
		print "<hr>";
		}
    #die($xml->Events->item[0]->EventName."<hr>");
}  
}

$karabas=new Karabas_Export;
die("done<hr>");
?>